package com.crm.system.controller.workOrderManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.workOrderManager.WorkOrder;
import com.crm.model.entity.workOrderManager.WorkOrderCreditDeductRecord;
import com.crm.model.entity.workOrderManager.WorkOrderReply;
import com.crm.model.qo.workOrderManager.WorkOrderQO;
import com.crm.model.vo.workOrderManager.WorkOrderReplyVO;
import com.crm.model.vo.workOrderManager.WorkOrderVO;
import com.crm.system.service.workOrderManager.WorkOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author baijian
 * @Date 2021/5/18 14:41
 */
@RestController
@RequestMapping("/workOrder")
@Slf4j
public class WorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    /**
     * 管理模块 -- 工单管理列表
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param workOrderQO 工单QO
     * @return
     */
    @PostMapping("/workOrderList")
    public DataResponse workOrderList(HttpServletRequest request,
                                         @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                         @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                         @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                         @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                         WorkOrderQO workOrderQO
    ) {
        try {
            DhPageInfo<WorkOrderVO> workOrderVOList = workOrderService.workOrderList(pageIndex, pageSize, sortName, sortOrder,workOrderQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), workOrderVOList);
        } catch (Exception e) {
            log.error("workOrder workOrderList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 工单管理-新增工单
     * @param workOrder 工单实体类
     * @return
     */
    @PostMapping("/addWorkOrder")
    public DataResponse addWorkOrder(@RequestBody WorkOrder workOrder) {
        String result = workOrderService.addWorkOrder(workOrder);
        if("WO0002".equals(result)){
            return new DhResponse(ResponseCodeEnum.WORKORDER_CREDIT.getResponseCode());
        }
        if("SUCCESS".equals(result)){
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }

    /**
     * 工单管理-工单列表-回复工单
     * @param workOrderReply 工单实体类
     * @return
     */
    @PostMapping("/addWorkOrderReply")
    public DataResponse addWorkOrderReply(@RequestBody WorkOrderReply workOrderReply) {
        try{
            String result = workOrderService.addWorkOrderReply(workOrderReply);
            if("WO0001".equals(result)){
                return new DhResponse(ResponseCodeEnum.WORKORDER_EXCEED.getResponseCode());
            }
            if("SUCCESS".equals(result)){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }catch(Exception e){
            log.error("workOrder addWorkOrderReply error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 工单管理-工单列表-回复工单列表
     * @param workOrderId 工单id
     * @return
     */
    @PostMapping("/workOrderReplyList")
    public DataResponse workOrderReplyList(@RequestParam(value = "workOrderId", required = false) String workOrderId) {
        try{
            List<WorkOrderReplyVO> list = workOrderService.workOrderReplyList(workOrderId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), list);
        } catch (Exception e) {
            log.error("workOrder workOrderReplyList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 修改工单
     * @Author renshaorong
     * @Date 2021/7/20
     * @param workOrder
     * @return
     */
    @PostMapping("/updateWorkOrder")
    public DataResponse updateWorkOrder(@RequestBody WorkOrder workOrder) {
        return workOrderService.updateByPrimaryKeySelective(workOrder) ? new DhResponse(ResponseCodeEnum.OK.getResponseCode()) : new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }

    /**
     * 工单管理-超级管理员-扣除信用值
     * @param workOrderCreditDeductRecord 工单扣除信用值记录
     * @return
     */
    @PostMapping("/creditDeduct")
    public DataResponse creditDeduct(@RequestBody WorkOrderCreditDeductRecord workOrderCreditDeductRecord) {
        Boolean result = workOrderService.creditDeduct(workOrderCreditDeductRecord);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        } else {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }



}
